﻿using IOA.MES.Business;
using IOA.MES.ClientApi.Models.Request.Auth;
using IOA.MES.ClientApi.Models.Response.Auth;
using IOA.MES.Common;
using IOA.MES.Log.DataAccess.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.ClientApi.Controllers
{
    public class AuthController : ApiController
    {
        /// <summary>
        /// 用户登陆
        /// </summary>
        [HttpPost]
        public ResultJson<LoginResponse> Login([FromBody] LoginRequest request)
        {
            var userInfo = new UserMgr().GetUser(0, request.UserName);
            if (userInfo == null)
            {
                throw new MultiLanBizException(105002);//用户名或密码错误
            }
            else if (!userInfo.Roles.Any(_ => _.PkId == 1))
            {
                if (!userInfo.Workshops.Any(_ => string.Equals(BaseEnv.WorkshopCode, _.WorkshopCode, StringComparison.CurrentCultureIgnoreCase)))
                {
                    throw new MultiLanBizException(105011);//您没有当前车间的权限
                }
                else if (!userInfo.Sites.Select(_ => _.Key).Contains(request.MachineType))
                {
                    throw new MultiLanBizException(105012);//您没有当前站点的权限
                }
            }

            var user = new UserMgr().Login(request.UserName, request.Password, "简体中文", new Log.DataAccess.VModel.VLoginHistory
            {
                ClientType = ClientType.MachineClient,
                DeviceType = DeviceType.Windows,
                ClientIP = BaseEnv.ClientIP,
                UserAgent = System.Web.HttpContext.Current.Request.UserAgent?.ToLower() ?? string.Empty
            }, true);

            var ftpConfig = new WorkshopMgr().GetFtpConfig(BaseEnv.WorkshopCode);
            return new ResultJson<LoginResponse>
            {
                Data = new LoginResponse
                {
                    TrueName = user.TrueName,
                    Token = user.Token,
                    Language = user.Language,
                    Roles = user.Roles.Select(_ => new Role { RoleId = _.PkId, RoleName = _.RoleName }).ToList(),
                    Workshops = user.Workshops.Select(_ => new SimpleWorkshop { WorkshopCode = _.WorkshopCode, WorkshopName = _.WorkshopName }).ToList(),
                    FtpConfig = ftpConfig == null ? null : new FtpConfig
                    {
                        FtpRoot = ftpConfig.FtpRoot,
                        UserName = ftpConfig.UserName,
                        Password = ftpConfig.Password
                    }
                }
            };
        }

        /// <summary>
        /// 查询班次列表
        /// </summary>
        [HttpPost]
        public ResultJson<QueryDutiesResponse> QueryDuties([FromBody] QueryDutiesRequest request)
        {
            var count = 0;
            var duties = new WorkshopDutyMgr().QueryDuties(1, 0, out count, request.WorkshopCode, DataAccess.Enum.CommonStatus.Normal);
            return new ResultJson<QueryDutiesResponse>(new QueryDutiesResponse
            {
                Duties = duties.Select(_ => new Duty
                {
                    DutyId = _.PkId,
                    DutyName = _.DutyName,
                    WorkshopCode = _.WorkshopCode,
                    WorkshopName = _.WorkshopName
                }).ToList()
            }, count);
        }

        /// <summary>
        /// Ping接口
        /// </summary>
        [HttpPost]
        public ResultJson<string> Ping([FromBody] object request)
        {
            try
            {
                var users = new UserMgr().QueryUsers(new List<int> { 1 });
                return new ResultJson<string>();
            }
            catch
            {
                return new ResultJson<string>(500, "数据库异常");
            }
        }
    }
}
